package com.shine.app.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.MyReviewBean;
import com.shine.app.bean.request.ReviewsUpdateBean;
import com.shine.app.bean.responses.ValidateResponse;
import com.shine.app.commons.StringUtils;
import com.shine.app.commons.UIHelper;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ViewMyInfoReview_editActivity extends BaseActivity implements
		OnClickListener {

	@InjectView(R.id.head_back)
	private ImageView head_backImageView;
	@InjectView(R.id.head_name)
	private TextView head_nameTextView;
	@InjectView(R.id.head_number)
	private TextView head_numberTextView;
	@InjectView(R.id.head_save)
	private TextView head_saveTextView;

	@InjectView(R.id.progressBar)
	protected ProgressBar progressBar;
	@InjectView(R.id.content)
	private EditText contentEditText;
	@InjectView(R.id.ratingbar)
	private RatingBar ratingbar;
	private MyReviewBean myReviewBean;
	private CourseReviewEditAsyncTask courseReviewEditAsyncTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.viewmyinfo_reviews_edit);
		Injector.get(this).inject();
		init();
	}

	private void init() {
		myReviewBean = (MyReviewBean) getIntent().getSerializableExtra(
				"myReviewBean");
		head_saveTextView.setVisibility(View.VISIBLE);
		head_nameTextView.setText(R.string.edit_review);
		contentEditText.setText(myReviewBean.getDescription());
		ratingbar.setRating(myReviewBean.getRating());
		progressBar.setVisibility(View.GONE);
		head_saveTextView.setOnClickListener(this);
		head_backImageView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.head_back:
			noSaveDialog();
			break;

		case R.id.head_save:
			inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
			String content = contentEditText.getText().toString().trim();
			if (StringUtils.isEmpty(content) || content.length() < 10) {
				UIHelper.toastDialog(this, R.string.msg_review_too_short,
						R.string.msg_yes);
				return;
			}
			ReviewsUpdateBean bean = new ReviewsUpdateBean();
			bean.setCourseId(myReviewBean.getCourseId());
			bean.setId(myReviewBean.getId());
			bean.setUserId(appContext.getLoginInfo().getUser().getUserId());
			bean.setDescription(content);
			bean.setRating(ratingbar.getRating());
			courseReviewEditAsyncTask = new CourseReviewEditAsyncTask();
			courseReviewEditAsyncTask.execute(bean);
			break;

		default:
			break;
		}
	}

	public class CourseReviewEditAsyncTask extends
			AsyncTask<Object, Integer, ValidateResponse> {
		private ReviewsUpdateBean updateBean;

		@Override
		protected ValidateResponse doInBackground(Object... params) {
			ValidateResponse response = null;
			updateBean = (ReviewsUpdateBean) params[0];
			response = appContext.updateCourseReview(updateBean);
			return response;
		}

		@Override
		protected void onPostExecute(ValidateResponse result) {
			super.onPostExecute(result);
			if (result != null && result.isData()) {
				UIHelper.toastDialog(ViewMyInfoReview_editActivity.this,
						R.string.msg_review_update_success, R.string.msg_yes);
				AppManager.getAppManager().finishActivity();
			} else {
				UIHelper.toastDialog(ViewMyInfoReview_editActivity.this,
						R.string.msg_review_update_failed, R.string.msg_yes);
			}
			resetViewStatus(true);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			resetViewStatus(false);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			noSaveDialog();
		}
		return false;
	}

	private void noSaveDialog() {
		Intent intent = new Intent(this,
				ViewMyInfo_ReviewNosaveDialogActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
		this.startActivity(intent);
	}

	private void resetViewStatus(boolean flag) {
		if (flag) {
			progressBar.setVisibility(View.GONE);
		} else {
			progressBar.setVisibility(View.VISIBLE);
		}
		head_backImageView.setClickable(flag);
		head_saveTextView.setClickable(flag);
		ratingbar.setEnabled(flag);
		contentEditText.setEnabled(flag);
	}

	@Override
	protected void onDestroy() {
		if (courseReviewEditAsyncTask != null
				&& courseReviewEditAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			courseReviewEditAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

}
